<?php

declare(strict_types=1);

namespace App\Request\Admin;

use App\Request\FormRequest;
use Hyperf\Validation\Rule;

class Role extends FormRequest
{
    /**
     * 场景
     * @var array|\string[][]
     */
    protected array $scenes = [
        'save' => [
            'name',
            'desc',
            'status',
            'permission',
            "permission.*",
            'menu',
            "menu.*"
        ],
        'update' => [
            'name',
            'desc',
            'status',
            'permission',
            "permission.*",
            'menu',
            "menu.*"
        ],
        'destroy' => [
            'ids',
            'ids.*'
        ]
    ];

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        $rules = [
            'name' => [
                'required',
                'max:15',
                Rule::unique('role')->ignore($this->route('id'))
            ],
            'desc' => 'max:80',
            'status' => 'in:0,1',
            'ids' => 'required|array',
            'ids.*' => 'required|numeric|min:1'
        ];

        if (getApp() === 'shop') {
            $rules = array_merge($rules, [
                'menu' => 'required|array',
                'menu.*' => 'required|numeric|min:1',
            ]);
        } else {
            $rules = array_merge($rules, [
                'permission' => 'required|array',
                'permission.*' => 'required|numeric|min:1',
            ]);
        }

        return $rules;
    }
}
